-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
WIP: Better formatting for .set_precision #11667
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
… and removed formatting from index.
…llow for tuple keys.
.set_precision
Thanks for putting this up. A bit busy ATM, but I'll try to review in the next couple weeks. Sorry for the wait! |
No worries I've been quite busy as well. There's a related issue from yesterday #11692 - the bottom line all the builtin styles, not just |
@@ -144,7 +151,18 @@ def __init__(self, data, precision=None, table_styles=None, uuid=None, | |||
self.table_styles = table_styles | |||
self.caption = caption | |||
if precision is None: | |||
precision = pd.options.display.precision | |||
precision = {'__default__': pd.options.display.precision} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just call .set_precision
instead of repeating this (and all of this logic should be there).
Further I think that a .set_precision()
should reset to the default (e.g. pd.options.display.precision
), so pls put in a test for this.
pls add some tests / whatsnew note enhancements / change the html-styling.ipynb to show the features as well. |
@mattilyra going to throw out some partially-formed thoughts here, let me know what you think. In light of #11692 (Styler not able to take formatters) I think we need a more general solution than what you've got here (but this will be part of that). I think we should determine the display value in the {% for c in r %} {# for each column in the row #}
<{{c.type}} id="T_{{uuid}}{{c.id}}" class="{{c.class}}">
{% if c.value is number %}
{{c.value|round(precision)}}
{# more elifs for each type of formatting? This will get ugly #}
{% else %}
{{c.value}}
{% endif %}
{% endfor %} We'd have {% for c in r %}
<{{c.type}} id="T_{{uuid}}{{c.id}}" class="{{c.class}}">
{{c.display_value}} {# <-------- The change is here #}
{% endfor %} We'd still need to define a nice API for users to map values to display values. Something like what you've got here would be good. And keeping shortcuts like |
@TomAugspurger I agree that separating the display value makes sense. I don't quite get the point of having I think you're second example should already be possible with the changes here, as you can pass arbitrary format strings as the format for each column. The logic is a little unclear in the template and should probably be moved to |
See my PR at #11768 Roughly what I envision is that PR will define an API for people to apply custom formatters on a column / subset basis.
|
I didn't notice that when I first looked through. Again, this hasn't fully formed in my mind yet, but do you think that |
What do you mean multiple formatters?
Yeah should be possible. Currently I hadn't thought that you would call If we can nail down how this whole thing should roughly work I'm happy to push up a proposal implementation for this. |
Nevermind about that, probably a bad idea.
Yes, I think you're right. Sorry about that this is all still coming together. The way I see
df.style.format('{:2f}') # equivalent to `.set_precision(2)`
I hate to make the work you've done here obsolete :/ but it looks like a more general |
Well it isn't really extra work as that's more or less how I think having the option os passing a callable as the formatter is also a good idea, it allows for a lot of flexibility. I think format callable could in that case take also the I'm happy to keep working on this. |
Feel free to repurpose this PR to take over For
Where
The trouble with this is that all styler functions written by users have to accept those as well, even if they don't use them, which could be ok. |
Well the user style functions can always take a I think I'll manage with git, merging your branch into this one. |
Conflicts: pandas/core/style.py
[ci skip] Conflicts: pandas/core/style.py
@mattilyra Have you had a chance to work on this any more? Any unforeseen obstacles coming up? I'm going to try and knock out sparse MultiIndices and truncated reprs in the next week or two, and will base it off the changes to the template. Just post here if you see any significant changes that are needed, or if you want to split that template change off into a separate pull request. |
@TomAugspurger status of this |
closing, but pls reopen if you'd like to update |
closes #11656
I found some extra time behind the sofa and started working on this. I made quite a few additions but by default everything should work as it did.
.set_precision
with anint
will truncate all columns to that many decimal places as beforedict
to.set_precision
int
s or valid pythonstr.format
strings{:.2e}
and possibly some string/date manipulations.set_precision
should perhaps be calledset_format
(orformat_cells
) as a result.set_precision()
(no args)what's missing?
pd.IndexSlice
for subsets?printing with just the defaults

crazy awesome new formatting (forget that the
kwarg
is calledcolumn_formats
that's been changed tosubsets
)